package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_TopicDao;
import com.hhit.collaborative_learning.dao.BBS_TopicStateDao;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicStateDaoImpl;
import com.hhit.collaborative_learning.entity.BBSTopicState;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.hhit.collaborative_learning.utils.StringUtil;

/**
 * Servlet implementation class SetTopicServlet
 */
@WebServlet("/community/SetTopicServlet")
public class SetTopicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SetTopicServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String[] idArray = request.getParameterValues("idArray[]");
		String tyle = request.getParameter("tyle");
		String tyleID = request.getParameter("tyleID");
		String idString = StringUtil.formatArraytoString(1, idArray, ",");
		System.out.println("topic ids "+idString);
		JSONObject jobj = new JSONObject();
		Connection con = DBAccess.getInstance().getConn();
		switch (tyleID) {
		case "-2":
			System.out.println("-2  清空状态");
			jobj=clearTopicState(con,idString);
			break;
		case "-1":
			System.out.println("-1 删除");
			jobj=deletTopic(con,idString);
			break;
		default:
			System.out.println("other "+tyle);
			jobj=setTopicStateTyle(con,idArray,tyleID);
			break;
		}
		try {
			ResponseUtil.write(response, jobj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
			System.out.println("respons object :"+jobj.toString());
		}
	}

	private JSONObject deletTopic(Connection con,String ids) {
		BBS_TopicDao td = new BBS_TopicDaoImpl();
		int re = td.delet(con, "ID", ids);
		JSONObject json = new JSONObject();
		if (re == 0) {
			json.put("isSuccess", "false");
		} else {
			json.put("isSuccess", "true");
		}
		json.put("delnum", re);
		return json;
	}
	private JSONObject clearTopicState(Connection con,String ids){
		BBS_TopicStateDao tsd=new BBS_TopicStateDaoImpl();
		int re=tsd.delet(con, "TopicID", ids);
		JSONObject json = new JSONObject();
		if (re == 0) {
			json.put("isSuccess", "false");
		} else {
			json.put("isSuccess", "true");
		}
		json.put("delnum", re);
		return json;
	}
	private JSONObject setTopicStateTyle(Connection con,String[] tIdArray,String tyleId){
		List<BBSTopicState> stateList=BBSTopicState.getlist(tIdArray, tyleId);
		BBS_TopicStateDao btsd=new BBS_TopicStateDaoImpl();
		int re=btsd.insert(con, stateList);
		JSONObject json = new JSONObject();
		if(re==0){
			json.put("isSuccess", "false");
		}else{
			json.put("isSuccess", "true");
		}
		json.put("insnum", re);
		return json;
	}
}
